package com.northming.daytodo.util;

import org.joda.time.DurationFieldType;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class TimeUtil {
	
	static DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); 
	
	/**
	 * get today yyyy-MM-dd
	 * @return
	 */
	public static String getToday(){
		return new LocalDate().toString();
	}
	
	public static String getOtherDay(int daySpan){		
		return new LocalDate().withFieldAdded(DurationFieldType.days(), daySpan).toString();
	}
	
	/**
	 * get this week days [yyyy-MM-dd,yyyy-MM-dd]
	 * @return String[]
	 */
	public static String[] getThisWeekDays(){
		String[] weekDays = new String[7];
		LocalDate localDate = new LocalDate();
		int dayOfWeek = localDate.getDayOfWeek();
		localDate = localDate.withFieldAdded(DurationFieldType.days(), -dayOfWeek);
		for (int i = 0; i < 7; i++) {
			localDate=localDate.withFieldAdded(DurationFieldType.days(),1);
			weekDays[i] = localDate.toString();
		}
		return weekDays;
	}
	/**
	 * get week days [yyyy-MM-dd,yyyy-MM-dd]
	 * @param year
	 * @param week
	 * @return String[]
	 */
	public static String[] getWeekDays(int year,int week){
		String[] weekDays = new String[7];
		LocalDate localDate = new LocalDate(year, 1, 1);
		int weekyear = localDate.getWeekyear();
		if(weekyear!=year){
			int dayOfWeek = localDate.getDayOfWeek();
			localDate = localDate.withFieldAdded(DurationFieldType.days(), +(7+1-dayOfWeek));
			
		}
		localDate = localDate.withFieldAdded(DurationFieldType.days(),+(7*(week-1)));
		weekDays[0] = localDate.toString();
		for (int i = 1; i < 7; i++) {
			localDate=localDate.withFieldAdded(DurationFieldType.days(),1);
			weekDays[i] = localDate.toString();
		}
		return weekDays;
	}
	/**
	 * 
	 * <pre>
	 * get now yyyy-MM-dd hh:mm:ss
	 * @return 
	 * 
	 * </pre>
	 *
	 */
	public static String getNow() {		
		return new LocalDateTime().toString(format);
	}
	
	
	
}
